package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.DbUtil;
import com.csjy.migration.hnnd.ProgramArgument;
import com.csjy.migration.hnnd.Tool;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;

import javax.script.ScriptException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@BusinessDataConverter(
    auto = false,
    insertSql = "INSERT INTO ND_BPM_RECOVERY_PROCEEDS\n" +
        "(ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, " +
        "PRO_CODE, ESTATE_CONTENT, DEAL_DATE, DEAL_VALUE, ESTATE_OWNER, DEAL_MAN, REMARK, " +
        "PRO_ID, IS_VALID, TASK_ID, PROC_ID, TASK_DEF_KEY, SOURCE_ID)\n" +
        "VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    updateSql = "UPDATE ND_BPM_RECOVERY_PROCEEDS\n" +
        "SET CREATE_NAME=?, CREATE_BY=?, CREATE_DATE=?, UPDATE_NAME=?, UPDATE_BY=?, UPDATE_DATE=?, SYS_ORG_CODE=?, SYS_COMPANY_CODE=?, BPM_STATUS=?, PRO_CODE=?, ESTATE_CONTENT=?, DEAL_DATE=?, DEAL_VALUE=?, ESTATE_OWNER=?, DEAL_MAN=?, REMARK=?, PRO_ID=?, IS_VALID=?, TASK_ID=?, PROC_ID=?, TASK_DEF_KEY=?, SOURCE_ID=?\n" +
        "WHERE ID = ?",
    findMigratedIdListSql = "SELECT ID FROM ND_BPM_RECOVERY_PROCEEDS WHERE PROC_ID = ?"
)
public class NdBpmRecoveryProceeds extends AbstractWorkflowBusiness {

  public NdBpmRecoveryProceeds(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId, String procInstId) throws SQLException {
    super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId, procInstId);
  }

  @Override
  public void findMigratedIdList() throws SQLException {
    // 查询已迁移的记录
    this.migratedIdList = this.targetDbUtil.selectSingleColumn(this.findMigratedIdListSql,
        Arrays.asList(new String[]{this.projectId}));  }

  @Override
  public void migrate() throws Exception {

    for(Map.Entry<String,Map<String,Object>> entry : taskParamMap.entrySet()) {
      String curJbpmTaskInstanceId = entry.getKey();
      Map<String,Object> taskParam = entry.getValue();
      String DeleteSql ="DELETE FROM ND_BPM_RECOVERY_PROCEEDS WHERE PRO_ID = ?";
      this.targetDbUtil.updateByParams(DeleteSql, Arrays.asList(new String[]{this.projectId}));
      for (Map row : this.sourceData.getBtItemForceRefundDetailList()) {
        try {

            this.targetDbUtil.updateByParams(insertSql,this.buildValues(row, curJbpmTaskInstanceId, taskParam));

        } catch(Exception e) {
          throw new Exception(String.format("流程实例[%s]-任务实例[%s]-收费退费数据迁移失败:%s\n",this.procInstId, curJbpmTaskInstanceId, e.getMessage()));
        }
      }
    }
  }

  public List buildValues(Map row, String curJbpmTaskInstanceId, Map<String,Object> taskParam) throws ScriptException, SQLException {
    List values = new ArrayList();
    // ID
    values.add(uuidGenerator.getNextId());
    // CREATE_NAME
    values.add(null);
    // CREATE_BY
    values.add(row.get("EXECUTPERSON"));
    // CREATE_DATE
    values.add(row.get("CREATE_TIME"));
    // UPDATE_NAME
    values.add(null);
    // UPDATE_BY
    values.add(null);
    // UPDATE_DATE
    values.add(row.get("MODIFY_TIME"));
    // SYS_ORG_CODE
    values.add(null);
    // SYS_COMPANY_CODE
    values.add("002");
    // BPM_STATUS
    values.add(1);
    // PRO_CODE 项目编号
    values.add(null);
    // ESTATE_CONTENT 财产内容
    values.add(row.get("ESTATENAME"));
    // DEAL_DATE 处置时间
    values.add(row.get("EXECUTTIME"));
    // DEAL_VALUE 处置价值|万
    values.add(row.get("PRICESUM"));
    // ESTATE_OWNER 财产所有者名
    values.add(row.get("OWNERNAME"));
    // DEAL_MAN 经办人员
    values.add(row.get("EXECUTPERSON"));
    // REMARK 备注信息
    values.add(row.get("MEMO"));
    // PRO_ID 项目ID
    values.add(row.get("ITEMNO"));
    // IS_VALID 是否使用
    values.add("Y");
    // TASK_ID 任务ID
    values.add(curJbpmTaskInstanceId + "-JW");
    // PROC_ID 流程ID
    values.add(this.procInstId+ "-JW");
    // TASK_DEF_KEY 节点key
    values.add(taskParam.get("taskDefKey"));
    // TODO:SOURCE_ID 业务ID
    values.add(null);
    return values;
  }
}
